package top.whysu.gps.controller.navigation;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.whysu.gps.constant.CodeConstant;
import top.whysu.gps.constant.DataDictionary;
import top.whysu.gps.constant.SystemConfigKey;
import top.whysu.gps.dto.generate.RequestDTO;
import top.whysu.gps.dto.generate.ResponseDTO;
import top.whysu.gps.dto.navigation.CurrentRouteAddModifyDTO;
import top.whysu.gps.dto.navigation.RouteByHandDTO;
import top.whysu.gps.dto.navigation.RouteStateUpdateDTO;
import top.whysu.gps.dto.navigation.SwitchModeDTO;
import top.whysu.gps.exception.BusinessException;
import top.whysu.gps.po.navigation.RouteCurrentPO;
import top.whysu.gps.service.navigation.NavigationService;
import top.whysu.gps.service.navigation.RouteCurrentService;
import top.whysu.gps.service.system.SystemConfigService;
import top.whysu.gps.utils.ResponseUtil;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;

/**
 * 航行管理--实时操作
 */
@Controller
@RequestMapping("/navigation")
public class NavigationController {

    @Resource
    private RouteCurrentService routeCurrentService;
    @Resource
    private SystemConfigService systemConfigService;
    @Resource
    private NavigationService navigationService;

    /**
     * 界面：航行管理--实时操作
     */
    @GetMapping("/navigationManager")
    public String navigationManager(Map<String, Object> map) {
        map.put(SystemConfigKey.CURRENT_MODE, systemConfigService.getConfig(SystemConfigKey.CURRENT_MODE));
        map.put(SystemConfigKey.CURRENT_MODE_STATUS, systemConfigService.getConfig(SystemConfigKey.CURRENT_MODE_STATUS));
        map.put(SystemConfigKey.CURRENT_NAVIGATION_ID, systemConfigService.getConfig(SystemConfigKey.CURRENT_NAVIGATION_ID));
        map.put(SystemConfigKey.Common.COMMON_COURSE, systemConfigService.getConfig(SystemConfigKey.Common.COMMON_COURSE));
        return "navigation/navigationManager";
    }

    /**
     * 界面：航行管理--实时操作--选择某点
     */
    @RequestMapping(value = "/routeSelectFrame")
    public String routeSelectFrame() {
        return "navigation/frame/routeSelectFrame";
    }

    /**
     * 界面：航行管理--实时操作--模式配置
     */
    @RequestMapping(value = "/modeConfigFrame")
    public String modeConfigFrame() {
        return "navigation/frame/modeConfigFrame";
    }

    /**
     * 界面：航行管理--实时操作--切换模式
     */
    @GetMapping(value = "/switchModeFrame")
    public String switchModeFrame(Map<String, Object> map) {
        map.put(SystemConfigKey.CURRENT_MODE, systemConfigService.getConfig(SystemConfigKey.CURRENT_MODE));
        map.put(SystemConfigKey.CURRENT_MODE_STATUS, systemConfigService.getConfig(SystemConfigKey.CURRENT_MODE_STATUS));
        map.put(SystemConfigKey.CURRENT_NAVIGATION_ID, systemConfigService.getConfig(SystemConfigKey.CURRENT_NAVIGATION_ID));
        return "navigation/frame/switchModeFrame";
    }

    /**
     * 界面：航行管理--实时操作--手动锚泊
     */
    @GetMapping(value = "/setAnchorPointByHandFrame")
    public String setAnchorPointByHandFrame(Map<String, Object> map) {
        map.put(SystemConfigKey.Anchor.ANCHOR_POINT_BY_HAND, systemConfigService.getConfig(SystemConfigKey.Anchor.ANCHOR_POINT_BY_HAND));
        map.put(SystemConfigKey.Common.COMMON_COURSE, systemConfigService.getConfig(SystemConfigKey.Common.COMMON_COURSE));
        return "navigation/frame/setAnchorPointByHandFrame";
    }

    /**
     * 界面：航行管理--实时操作--手动漂浮
     */
    @GetMapping(value = "/setWanderPointByHandFrame")
    public String setWanderPointByHandFrame(Map<String, Object> map) {
        map.put(SystemConfigKey.CURRENT_NAVIGATION_ID, systemConfigService.getConfig(SystemConfigKey.CURRENT_NAVIGATION_ID));
        return "navigation/frame/setWanderPointByHandFrame";
    }

    /**
     * 界面：航行管理--实时操作--手动单点
     */
    @GetMapping(value = "/setSinglePointByHandFrame")
    public String setSinglePointByHandFrame(Map<String, Object> map) {
        map.put(SystemConfigKey.Single.SINGLE_POINT_BY_HAND, systemConfigService.getConfig(SystemConfigKey.Single.SINGLE_POINT_BY_HAND));
        map.put(SystemConfigKey.Common.COMMON_COURSE, systemConfigService.getConfig(SystemConfigKey.Common.COMMON_COURSE));
        return "navigation/frame/setSinglePointByHandFrame";
    }

    /**
     * 获取当前航线表
     */
    @RequestMapping("/getCurrentRouteList")
    @ResponseBody
    public ResponseDTO<List<RouteCurrentPO>> getPage() {
        return ResponseUtil.getSuccessResponse(routeCurrentService.selectList());
    }

    /**
     * 新增、修改当前航线表
     */
    @PostMapping("/addModifyCurrentRoute")
    @ResponseBody
    public ResponseDTO<BigDecimal> addModifyCurrentRoute(@RequestBody @Validated RequestDTO<CurrentRouteAddModifyDTO> requestDTO) {
        return ResponseUtil.getSuccessResponse(requestDTO.getHeader(), routeCurrentService.wholeAddModify(requestDTO.getBody()));
    }

    /**
     * 删除当前航线
     */
    @PostMapping("/deleteCurrentRoute")
    @ResponseBody
    public ResponseDTO deleteCurrentRoute(@RequestBody @Validated RequestDTO<BigDecimal> requestDTO) {
        routeCurrentService.deleteCurrentRoute(requestDTO.getBody());
        return ResponseUtil.getSuccessResponse(requestDTO.getHeader());
    }

    /**
     * 设置当前航线
     *
     * @param requestDTO 当前航线表的主键id（route_current）
     */
    @PostMapping("/setCurrentRoute")
    @ResponseBody
    public ResponseDTO setCurrentRoute(@RequestBody RequestDTO<List<BigDecimal>> requestDTO) {
        routeCurrentService.wholeSetCurrentRoute(requestDTO.getBody());
        return ResponseUtil.getSuccessResponse(requestDTO.getHeader());
    }

    /**
     * 批量删除当前航线
     *
     * @param requestDTO 需要批量删除的航线表的主键id（route_current）
     */
    @PostMapping("/batchDeleteCurrentRoute")
    @ResponseBody
    public ResponseDTO batchDeleteCurrentRoute(@RequestBody RequestDTO<List<BigDecimal>> requestDTO) {
        routeCurrentService.batchDeleteCurrentRoute(requestDTO.getBody());
        return ResponseUtil.getSuccessResponse(requestDTO.getHeader());
    }

    /**
     * 选择某点航线（当做漂浮模式/船舶模式/单点循环模式的点）
     */
    @PostMapping("/setCurrentSingleRoute")
    @ResponseBody
    public ResponseDTO setCurrentSingleRoute(@RequestBody RequestDTO<BigDecimal> requestDTO) {
        // 将模式设置为【单点模式】
        systemConfigService.newTxUpdateConfig(SystemConfigKey.CURRENT_MODE, String.valueOf(DataDictionary.Mode.SINGLE_CIRCLE));
        systemConfigService.newTxUpdateConfig(SystemConfigKey.CURRENT_MODE_STATUS, String.valueOf(DataDictionary.ModeStatus.RUN));
        return ResponseUtil.getSuccessResponse(requestDTO.getHeader());
    }

    /**
     * 切换模式
     */
    @PostMapping("/switchMode")
    @ResponseBody
    public ResponseDTO switchMode(@RequestBody @Validated RequestDTO<SwitchModeDTO> requestDTO) {
        navigationService.wholeSwitchMode(requestDTO.getBody());
        return ResponseUtil.getSuccessResponse(requestDTO.getHeader());
    }

    /**
     * 更改航线状态
     */
    @PostMapping("/updateRouteState")
    @ResponseBody
    public ResponseDTO updateRouteState(@RequestBody @Validated RequestDTO<RouteStateUpdateDTO> requestDTO) {
        RouteStateUpdateDTO dto = requestDTO.getBody();
        routeCurrentService.updateNavigationState(dto.getId(), dto.getRouteState());
        return ResponseUtil.getSuccessResponse(requestDTO.getHeader());
    }

    /**
     * 导入航线表excel
     *
     * @param uploadFile 文件
     * @return json
     */
    @PostMapping("/importRouteExcel")
    @ResponseBody
    public ResponseDTO<String> importRouteExcel(@RequestParam("file") MultipartFile uploadFile) {
        navigationService.importRouteExcel(uploadFile);
        return ResponseUtil.getSuccessResponse();
    }

    /**
     * 设置手动锚泊的点位
     */
    @PostMapping("/setAnchorPointByHand")
    @ResponseBody
    public ResponseDTO setAnchorPointByHand(@RequestBody @Validated RequestDTO<RouteByHandDTO> requestDTO) {
        String data = JSON.toJSONString(requestDTO.getBody());
        if (StringUtils.isBlank(data)) {
            throw new BusinessException(CodeConstant.Http.INFO_ERROR, "参数为空！");
        }
        // 设置锚泊点位
        systemConfigService.newTxUpdateConfig(SystemConfigKey.Anchor.ANCHOR_POINT_BY_HAND, data);
        systemConfigService.newTxUpdateConfig(SystemConfigKey.Common.BY_HAND, String.valueOf(DataDictionary.YesOrNo.YES));
        // 将模式设置为【锚泊模式】
        systemConfigService.newTxUpdateConfig(SystemConfigKey.CURRENT_MODE, String.valueOf(DataDictionary.Mode.ANCHOR));
        systemConfigService.newTxUpdateConfig(SystemConfigKey.CURRENT_MODE_STATUS, String.valueOf(DataDictionary.ModeStatus.RUN));
        // 航向
        String courseStr = requestDTO.getBody().getCourse();
        Double course = Double.valueOf(courseStr);
        if (course < 0 || course > 359.9) {
            throw new BusinessException(CodeConstant.Http.INFO_ERROR, "航向值为0到359.9！支持1位小数");
        }
        systemConfigService.newTxUpdateConfig(SystemConfigKey.Common.COMMON_COURSE, new DecimalFormat("000.#").format(course));
        return ResponseUtil.getSuccessResponse(requestDTO.getHeader());
    }

    /**
     * 设置手动单点的点位
     */
    @PostMapping("/setSinglePointByHand")
    @ResponseBody
    public ResponseDTO setSinglePointByHand(@RequestBody @Validated RequestDTO<RouteByHandDTO> requestDTO) {
        String data = JSON.toJSONString(requestDTO.getBody());
        if (StringUtils.isBlank(data)) {
            throw new BusinessException(CodeConstant.Http.INFO_ERROR, "参数为空！");
        }
        // 设置单点点位
        systemConfigService.newTxUpdateConfig(SystemConfigKey.Single.SINGLE_POINT_BY_HAND, data);
        systemConfigService.newTxUpdateConfig(SystemConfigKey.Common.BY_HAND, String.valueOf(DataDictionary.YesOrNo.YES));
        // 将模式设置为【单点模式】
        systemConfigService.newTxUpdateConfig(SystemConfigKey.CURRENT_MODE, String.valueOf(DataDictionary.Mode.SINGLE_CIRCLE));
        systemConfigService.newTxUpdateConfig(SystemConfigKey.CURRENT_MODE_STATUS, String.valueOf(DataDictionary.ModeStatus.RUN));
        // 航向
        systemConfigService.newTxUpdateConfig(SystemConfigKey.Common.COMMON_COURSE, requestDTO.getBody().getCourse());
        return ResponseUtil.getSuccessResponse(requestDTO.getHeader());
    }

    /**
     * 获取航线漂浮模式/船舶模式/单点循环模式选择的点
     */
    @PostMapping("/getSelectedPoint")
    @ResponseBody
    public ResponseDTO<RouteCurrentPO> getSelectedPoint(@RequestBody RequestDTO requestDTO) {
        return ResponseUtil.getSuccessResponse(requestDTO.getHeader(), routeCurrentService.getSelectedPoint());
    }

}
